﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Game : MonoBehaviour
{
    GameObject player;
    private static Game _instance;
    public static Game Instance
    {
        get
        {
            return _instance;
        }
    }

    private Vector3[] _bornPositons = { new Vector3(-10, 8, 0), new Vector3(0, 8, 0), new Vector3(10, 8, 0) };
    private Vector3 _bornPosition;
    private void Awake()
    {
        // player = GameObject.FindGameObjectWithTag("MyTank");
        _instance = this;

    }

    void Start()
    {
        BeginGame();
    }

    void Update()
    {

    }
    public void BeginGame()
    {
        EffectManager.Instance.PlayEffect("Born", new Vector3(-2, -8, 0), Quaternion.identity, 1);
        Invoke("BornPlayer", 1);
        InvokeRepeating("RundomBornEnemy", 0, 5);
    }

    public void BornPlayer()
    {
        GameObject playerPrefab = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Tank/Player.prefab", typeof(GameObject)) as GameObject;
        player = GameObject.Instantiate(playerPrefab, new Vector3(-2, -8, 0), Quaternion.Euler(new Vector3(0,0,0)));
    }

    public void RundomBornEnemy()
    {
        int pos = Random.Range(0, 3);
        _bornPosition = _bornPositons[pos];
        EffectManager.Instance.PlayEffect("Born", _bornPosition, Quaternion.identity, 1);
        Invoke("BornEnemy", 1);
    }

    public void BornEnemy()
    {
        int num = Random.Range(0, 2);
        if (num == 0)
        {
            GameObject enemyPrefab = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Tank/Enemy.prefab", typeof(GameObject)) as GameObject;
            GameObject.Instantiate(enemyPrefab, _bornPosition, Quaternion.Euler(new Vector3(0, 0, -180)));
        }
        else
        {
            GameObject enemyPrefab1 = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Tank/Enemy1.prefab", typeof(GameObject)) as GameObject;
            GameObject.Instantiate(enemyPrefab1, _bornPosition, Quaternion.Euler(new Vector3(0, 0, -180)));
        }
    }

    public void GameOver()
    {
        Debug.Log("游戏结束");
        ChangeHeartSprite();
        //player.GetComponent<Player>().Die();
    }

    void ChangeHeartSprite()
    {
        GameObject heart = GameObject.FindGameObjectWithTag("Heart");
        if (heart == null)
        {
            Debug.Log("老家没找到");
            return;
        }
        SpriteRenderer sr = heart.GetComponent<SpriteRenderer>();
        Sprite[] mapSprite = UnityEditor.AssetDatabase.LoadAllAssetsAtPath("Assets/GameResource/Graphics/Map.bmp").OfType<Sprite>().ToArray();
        sr.sprite = mapSprite[6];
    }
}
